package co.com.inventarios.persistencia;

import co.com.inventarios.modelo.Dlluser;
import co.com.inventarios.modelo.RequiPro;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Set;
import java.util.logging.Level;
import javax.persistence.EntityManager;
import javax.persistence.Query;

/**
 * A data access object (DAO) providing persistence and search support for
 * Dlluser entities. Transaction control of the save(), update() and delete()
 * operations must be handled externally by senders of these methods or must be
 * manually added to each of these methods for data to be persisted to the JPA
 * datastore.
 * 
 * @see co.com.inventarios.modelo.Dlluser
 * @author MyEclipse Persistence Tools
 */

public class DlluserDAO implements IDlluserDAO {
	// property constants
	public static final String USRNOMBRE = "usrnombre";
	public static final String USRPASSWD = "usrpasswd";
	public static final String USRPERFIL = "usrperfil";
	public static final String USRDIASVEN = "usrdiasven";
	public static final String USRPASSINT = "usrpassint";
	public static final String USRBODEGA = "usrbodega";
	public static final String USRACTIVO = "usractivo";
	public static final String USRBODENT = "usrbodent";
	public static final String USRBODSAL = "usrbodsal";
	public static final String USRTDCAUT = "usrtdcaut";

	private EntityManager getEntityManager() {
		return EntityManagerHelper.getEntityManager();
	}

	/**
	 * Perform an initial save of a previously unsaved Dlluser entity. All
	 * subsequent persist actions of this entity should use the #update()
	 * method. This operation must be performed within the a database
	 * transaction context for the entity's data to be permanently saved to the
	 * persistence store, i.e., database. This method uses the
	 * {@link javax.persistence.EntityManager#persist(Object) EntityManager#persist}
	 * operation.
	 * 
	 * <pre>
	 * EntityManagerHelper.beginTransaction();
	 * DlluserDAO.save(entity);
	 * EntityManagerHelper.commit();
	 * </pre>
	 * 
	 * @param entity
	 *            Dlluser entity to persist
	 * @throws RuntimeException
	 *             when the operation fails
	 */
	public void save(Dlluser entity) {
		EntityManagerHelper.log("saving Dlluser instance", Level.INFO, null);
		try {
			getEntityManager().persist(entity);
			EntityManagerHelper.log("save successful", Level.INFO, null);
		} catch (RuntimeException re) {
			EntityManagerHelper.log("save failed", Level.SEVERE, re);
			throw re;
		}
	}

	/**
	 * Delete a persistent Dlluser entity. This operation must be performed
	 * within the a database transaction context for the entity's data to be
	 * permanently deleted from the persistence store, i.e., database. This
	 * method uses the
	 * {@link javax.persistence.EntityManager#remove(Object) EntityManager#delete}
	 * operation.
	 * 
	 * <pre>
	 * EntityManagerHelper.beginTransaction();
	 * DlluserDAO.delete(entity);
	 * EntityManagerHelper.commit();
	 * entity = null;
	 * </pre>
	 * 
	 * @param entity
	 *            Dlluser entity to delete
	 * @throws RuntimeException
	 *             when the operation fails
	 */
	public void delete(Dlluser entity) {
		EntityManagerHelper.log("deleting Dlluser instance", Level.INFO, null);
		try {
			entity = getEntityManager().getReference(Dlluser.class,
					entity.getUsrcodigo());
			getEntityManager().remove(entity);
			EntityManagerHelper.log("delete successful", Level.INFO, null);
		} catch (RuntimeException re) {
			EntityManagerHelper.log("delete failed", Level.SEVERE, re);
			throw re;
		}
	}

	/**
	 * Persist a previously saved Dlluser entity and return it or a copy of it
	 * to the sender. A copy of the Dlluser entity parameter is returned when
	 * the JPA persistence mechanism has not previously been tracking the
	 * updated entity. This operation must be performed within the a database
	 * transaction context for the entity's data to be permanently saved to the
	 * persistence store, i.e., database. This method uses the
	 * {@link javax.persistence.EntityManager#merge(Object) EntityManager#merge}
	 * operation.
	 * 
	 * <pre>
	 * EntityManagerHelper.beginTransaction();
	 * entity = DlluserDAO.update(entity);
	 * EntityManagerHelper.commit();
	 * </pre>
	 * 
	 * @param entity
	 *            Dlluser entity to update
	 * @returns Dlluser the persisted Dlluser entity instance, may not be the
	 *          same
	 * @throws RuntimeException
	 *             if the operation fails
	 */
	public Dlluser update(Dlluser entity) {
		EntityManagerHelper.log("updating Dlluser instance", Level.INFO, null);
		try {
			Dlluser result = getEntityManager().merge(entity);
			EntityManagerHelper.log("update successful", Level.INFO, null);
			return result;
		} catch (RuntimeException re) {
			EntityManagerHelper.log("update failed", Level.SEVERE, re);
			throw re;
		}
	}

	public Dlluser findById(String id) {
		EntityManagerHelper.log("finding Dlluser instance with id: " + id,
				Level.INFO, null);
		getEntityManager().clear();
		try {
			Dlluser instance = getEntityManager().find(Dlluser.class, id);
			return instance;
		} catch (RuntimeException re) {
			EntityManagerHelper.log("find failed", Level.SEVERE, re);
			throw re;
		}
	}

	/**
	 * Find all Dlluser entities with a specific property value.
	 * 
	 * @param propertyName
	 *            the name of the Dlluser property to query
	 * @param value
	 *            the property value to match
	 * @return List<Dlluser> found by query
	 */
	@SuppressWarnings("unchecked")
	public List<Dlluser> findByProperty(String propertyName, final Object value) {
		EntityManagerHelper.log("finding Dlluser instance with property: "
				+ propertyName + ", value: " + value, Level.INFO, null);
		try {
			getEntityManager().clear();
			final String queryString = "select model from Dlluser model where model."
					+ propertyName + "= :propertyValue";
			Query query = getEntityManager().createQuery(queryString);
			query.setParameter("propertyValue", value);
			return query.getResultList();
		} catch (RuntimeException re) {
			EntityManagerHelper.log("find by property name failed",
					Level.SEVERE, re);
			throw re;
		}
	}

	public List<Dlluser> findByUsrnombre(Object usrnombre) {
		return findByProperty(USRNOMBRE, usrnombre);
	}

	public List<Dlluser> findByUsrpasswd(Object usrpasswd) {
		return findByProperty(USRPASSWD, usrpasswd);
	}

	public List<Dlluser> findByUsrperfil(Object usrperfil) {
		return findByProperty(USRPERFIL, usrperfil);
	}

	public List<Dlluser> findByUsrdiasven(Object usrdiasven) {
		return findByProperty(USRDIASVEN, usrdiasven);
	}

	public List<Dlluser> findByUsrpassint(Object usrpassint) {
		return findByProperty(USRPASSINT, usrpassint);
	}

	public List<Dlluser> findByUsrbodega(Object usrbodega) {
		return findByProperty(USRBODEGA, usrbodega);
	}

	public List<Dlluser> findByUsractivo(Object usractivo) {
		return findByProperty(USRACTIVO, usractivo);
	}

	public List<Dlluser> findByUsrbodent(Object usrbodent) {
		return findByProperty(USRBODENT, usrbodent);
	}

	public List<Dlluser> findByUsrbodsal(Object usrbodsal) {
		return findByProperty(USRBODSAL, usrbodsal);
	}

	public List<Dlluser> findByUsrtdcaut(Object usrtdcaut) {
		return findByProperty(USRTDCAUT, usrtdcaut);
	}

	/**
	 * Find all Dlluser entities.
	 * 
	 * @return List<Dlluser> all Dlluser entities
	 */
	@SuppressWarnings("unchecked")
	public List<Dlluser> findAll() {
		EntityManagerHelper.log("finding all Dlluser instances", Level.INFO,
				null);
		try {
			 List<Dlluser> reg = new ArrayList<Dlluser>();
			 
			getEntityManager().clear();
			 
			final String queryString = "select model from Dlluser model";
			Query query = getEntityManager().createQuery(queryString);
			
//			   reg = query.getResultList();
//			   for(Dlluser obj : reg){
//				//   System.out.println("Numero lineas Usuario -> "+obj.getUsrcodigo()+" Documentos:"+obj.getUsrbodaut());
//			   }
//			   reg.clear();
			   
			return query.getResultList();
		} catch (RuntimeException re) {
			EntityManagerHelper.log("find all failed", Level.SEVERE, re);
			throw re;
		}
	}

}